package com.cn.wanxi.service.impl; /***********************************************************************
 * Module:  UserServiceImpl.java
 * Author:  Administrator
 * Purpose: Defines the Class UserServiceImpl
 ***********************************************************************/

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.*;

/**
 * 服务逻辑层
 * 1.封装逻辑代码
 * 2.调用数据实现层
 */
public class UserServiceImpl implements UserService {

    @Override
    public ResultModel login(UserModel model) {
        UserDao userDao = new UserImpl();
        UserModel userModel = userDao.findByUsernameAndPassword(model);
//        java 优先级的计算
        boolean result = userModel != null;
        if (result) {
            return new ResultModel("success");
        }
        return new ResultModel("error");
    }

    @Override
    public String add(UserModel userModel) {
        UserDao userDao = new UserImpl();
        int add = userDao.add(userModel);
        return add > 0 ? "success" : "error";
    }

    @Override
    public String update(UserModel userModel) {
        UserDao userDao = new UserImpl();
        int add = userDao.update(userModel);
        return add > 0 ? "success" : "error";
    }

    @Override
    public String delete(UserModel userModel) {
        UserDao userDao = new UserImpl();
        int add = userDao.delete(userModel);
        return add > 0 ? "success" : "error";
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return new ResultModel(0, "", userDao.findAll(userModel), userDao.count(userModel));
    }

    @Override
    public UserModel findById(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.findById(userModel);
    }

    @Override
    public int count(UserModel userModel) {
        UserDao userDao = new UserImpl();
        return userDao.count(userModel);
    }
}